import { getUrlValue, setToken, getToken } from '@/utils/tools'
import { useGlobalStore } from '@/store/global'
import config from '@/config'
import { redirectLogin } from '@/utils/tools'

/**
 * 用户信息&权限校验
 */
export async function verifyAuth() {
    const globalStore = useGlobalStore()
    // 适配宽度，选出最大宽度进行逻辑判断
    if (!window.__MICRO_APP_ENVIRONMENT__) {
        let screenWidth = window.screen.width > document.body.clientWidth ? window.screen.width : document.body.clientWidth
        screenWidth = window.innerWidth > screenWidth ? window.innerWidth : screenWidth
        if (screenWidth >= 3840) {
            document.body.style.width = screenWidth + 'px'
            globalStore.changeScreenRatio16_9(false)
        }
    }

    let isOk = true
    try {
        // 修改系统标题
        document.title = config.systemTitle
        // 获取登录token
        const token = getUrlValue('token')
        if (token) {
            setToken(token)
        } else if (!getToken()) {
            // 没有token，跳转到登录页
            window.location.href = config.loginURL
        }
    } catch (error) {
        console.error(error)
        isOk = false
    }
    return isOk
}
